import 'package:dio/dio.dart' as dio;
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../api/user.dart';
import '../../utils/http_util.dart';
import '../../utils/my_colors.dart';
import '../../utils/my_icons.dart';
import 'controller.dart';


class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  late MainController controller;

  @override
  void initState() {
    super.initState();
    controller = Get.put(MainController());
  }

  void onDestinationSelected(int value) {
    // 点击当前 NavigationBar
    if (value == controller.selectedIndex.value) {
      var currentPage = controller.pages[value];
      // 对于首页和动态页面的特殊处理
      // 你的逻辑可以在这里展开
    }
    controller.selectedIndex.value = value;
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  // 主视图
  Widget buildView() {
    return Obx(() => Scaffold(
          extendBody: true,
          extendBodyBehindAppBar: true,
          primary: true,
          body: Row(
            children: [
              Expanded(
                child: Obx(
                  () => IndexedStack(
                    index: controller.selectedIndex.value,
                    children: controller.pages,
                  ),
                ),
              ),
            ],
          ),
          bottomNavigationBar: MediaQuery.of(context).size.width < 640
              ? NavigationBar(
                  height: 64,
                  destinations: [
                    NavigationDestination(
                      icon: Icon(
                        IconFontIcons.iconShouye1,
                        color: controller.selectedIndex.value == 0
                            ? MyColors.boyPink
                            : MyColors.grey,
                      ),
                      label: "首页",
                    ),
                    NavigationDestination(
                      icon: Icon(
                        IconFontIcons.iconFengche,
                        color: controller.selectedIndex.value == 1
                            ? MyColors.boyPink
                            : MyColors.grey,
                      ),
                      label: "动态",
                    ),
                    NavigationDestination(
                      icon: Icon(
                        IconFontIcons.iconJiahao,
                        color: controller.selectedIndex.value == 2
                            ? MyColors.boyPink
                            : MyColors.boyPink,
                      ),
                      label: "投稿",
                    ),
                    NavigationDestination(
                      icon: Icon(
                        IconFontIcons.iconPindao,
                        color: controller.selectedIndex.value == 3
                            ? MyColors.boyPink
                            : MyColors.grey,
                      ),
                      label: "频道",
                    ),
                    NavigationDestination(
                      icon: Icon(
                        IconFontIcons.iconAcfun2,
                        color: controller.selectedIndex.value == 4
                            ? MyColors.boyPink
                            : MyColors.grey,
                      ),
                      label: "我的",
                    ),
                  ],
                  selectedIndex: controller.selectedIndex.value,
                  onDestinationSelected: (value) =>
                      onDestinationSelected(value),
                  backgroundColor: Colors.white,
                )
              : null,
        ));
  }

  @override
  Widget build(BuildContext context) {
    return buildView(); // 确保返回正确的 widget
  }
}
